{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial 9\n",
    "\n",
    "May 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing the libraries \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from math import exp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "## Euler's method\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(t, y): \n",
    "    return (t**2 + 1) \n",
    "      \n",
    "        \n",
    "class Euler():\n",
    "    def __init__(self, t0, y0, h, func=func):\n",
    "        self.t0 = t0\n",
    "        self.y0 = y0\n",
    "        self.h = h\n",
    "        self.func = func\n",
    "  \n",
    "    def euler(self, t): \n",
    "        temp = -0\n",
    "        y = self.y0\n",
    "        t0 = self.t0\n",
    "        while t0 < t: \n",
    "            y = y + self.h * self.func(t0, y) \n",
    "            t0 = t0 + self.h \n",
    "        return(float(y))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mid Point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Midpoint():\n",
    "    def __init__(self, t0, y0, h, func=func):\n",
    "        self.t0 = t0\n",
    "        self.y0 = y0\n",
    "        self.h = h\n",
    "        self.func = func\n",
    "  \n",
    "    def midpoint(self, t): \n",
    "        y = self.y0\n",
    "        t0 = self.t0\n",
    "        while t0 < t: \n",
    "            y = y + self.h * self.func(t0+(self.h/2), y+ (self.h/2)*self.func(t0,y)) \n",
    "            t0 = t0 + self.h \n",
    "        return(float(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Heun's Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Heun():\n",
    "    def __init__(self, t0, y0, h, func=func):\n",
    "        self.t0 = t0\n",
    "        self.y0 = y0\n",
    "        self.h = h\n",
    "        self.func = func\n",
    "  \n",
    "    def heun(self, t): \n",
    "        y = self.y0\n",
    "        t0 = self.t0\n",
    "        while t0 < t: \n",
    "            y = y + self.h/2 *(self.func(t0,y) +self.func(t0 + self.h, y+ self.h*self.func(t0,y) )) \n",
    "            t0 = t0 + self.h\n",
    "        return(float(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RK4 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "class RK4():\n",
    "    def __init__(self, t0, y0, h, func=func):\n",
    "        self.t0 = t0\n",
    "        self.y0 = y0\n",
    "        self.h = h\n",
    "        self.func = func\n",
    "        \n",
    "\n",
    "    def increment_rk4(self,y, t):\n",
    "        ''' Runge Kutta 4 Discrete step '''\n",
    "        k1 = self.h * self.func(t, y)\n",
    "        X1 = y+ k1 / 2.\n",
    "        k2 = self.h * self.func( t + self.h / 2., X1)\n",
    "        X2= y+ k2 / 2.\n",
    "        k3 = self.h * self.func( t + self.h / 2., X2)\n",
    "        X3= y+ k3\n",
    "        k4 = self.h * self.func( t + self.h, X3)\n",
    "        y = y+ (k1 + 2 * k2 + 2 * k3 + k4) / 6.\n",
    "        return y\n",
    "\n",
    "    def solve_rk4(self,t):\n",
    "        y = self.y0\n",
    "        t0 = self.t0\n",
    "        while t0 < t: \n",
    "            y =  self.increment_rk4(y, t)\n",
    "            t0 = t0 + self.h\n",
    "        return(float(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercice 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(t, y): \n",
    "    return (3*y) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sol(t):\n",
    "    return(2* np.exp(3*t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exact solution : 2.6997176151520064\n",
      "Euler : 2.6\n",
      "Mid point : 2.69\n",
      "Heun : 2.69\n",
      "rk4 : 2.699675\n"
     ]
    }
   ],
   "source": [
    "h=0.1\n",
    "t=0.1\n",
    "print('Exact solution :', sol(t))\n",
    "method = Euler(0, 2, h, func)\n",
    "print('Euler :', method.euler(t))\n",
    "method = Midpoint(0, 2, h, func)\n",
    "print('Mid point :', method.midpoint(t))\n",
    "method = Heun(0, 2, h, func)\n",
    "print('Heun :' ,method.heun(t))\n",
    "method = RK4(0, 2, h, func)\n",
    "print('rk4 :' ,solver.solve_rk4(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exact solution : 3.644237600781018\n",
      "Euler : 3.3800000000000003\n",
      "Mid point : 3.61805\n",
      "Heun : 3.61805\n",
      "rk4 : 3.6441225528125\n"
     ]
    }
   ],
   "source": [
    "h=0.1\n",
    "t=0.2\n",
    "print('Exact solution :', sol(t))\n",
    "method = Euler(0, 2, h, func)\n",
    "print('Euler :', method.euler(t))\n",
    "method = Midpoint(0, 2, h, func)\n",
    "print('Mid point :', method.midpoint(t))\n",
    "method = Heun(0, 2, h, func)\n",
    "print('Heun :' ,method.heun(t))\n",
    "method = RK4(0, 2, h, func)\n",
    "print('rk4 :' ,solver.solve_rk4(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
